package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.mapper.SpecificationsMapper;
import com.hqyj.pojo.Specifications;
import com.hqyj.service.SpecificationsService;
import com.hqyj.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SpecificationsServiceImpl
 * @Description TODO
 * @Author ZhuYouWan
 * @Date 2022/9/2 11:00
 * @Version 1.0
 **/
@Service
public class SpecificationsServiceImpl implements SpecificationsService {
    private static final int CACHE_LIFE_TIME = 60*10;

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SpecificationsMapper specificationsMapper;
    private Map<String,Object> map;
    @Override
    public Map<String, Object> findAllGSpecifications(int pageNum, int pageSize, String condition) {

        String key = "allGSpecifications" + pageNum;

        if (!"".equals(condition)){
            PageHelper.startPage(pageNum,pageSize);
            List<Specifications> specificationsList = specificationsMapper.findAllGSpecifications(condition);
            PageInfo<Specifications> specificationsPageInfo = new PageInfo<>(specificationsList);
            map = new HashMap<>();
            map.put("code",200);
            map.put("specInfo",specificationsPageInfo);
        }else if (!redisUtil.hasKey(key)){
            PageHelper.startPage(pageNum,pageSize);
            List<Specifications> specificationsList = specificationsMapper.findAllGSpecifications(condition);
            PageInfo<Specifications> specificationsPageInfo = new PageInfo<>(specificationsList);
            map = new HashMap<>();
            map.put("code",200);
            map.put("specInfo",specificationsPageInfo);
            redisUtil.hmset(key,map,CACHE_LIFE_TIME);
        }else {
            map = redisUtil.hmget(key);
        }

        return map;
    }

    @Override
    public Map<String, Object> deleteGSpecificationsById(int sId) {
        Integer ret = specificationsMapper.deleteGSpecificationsById(sId);
        map = new HashMap<>();
        if (specificationsMapper.findGoodsContactBySidCount(sId)>0){
            specificationsMapper.updateGoodsContactBysId(sId);
            map.put("cascadeState",true);
        }else {
            map.put("cascadeState",false);
        }
        if (ret>0){
            delCache();
            redisUtil.del(redisUtil.keysLike("goodsAll"));
            map.put("code",200);
            map.put("message","删除成功");
        }else {
            map.put("code",-1);
            map.put("message","删除失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> insertGSpecifications(Specifications specifications) {
        Integer ret = specificationsMapper.insertGSpecifications(specifications);
        map = new HashMap<>();
        if (ret>0){
            delCache();
            redisUtil.del(redisUtil.keysLike("goodsAll"));
            map.put("code",200);
            map.put("message","添加成功");
        }else {
            map.put("code",-1);
            map.put("message","添加失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> findGSpecificationsById(int sId) {
        Specifications specificationsById = specificationsMapper.findGSpecificationsById(sId);
        map = new HashMap<>();
        if (specificationsById != null) {
            map.put("code",200);
            map.put("spec",specificationsById);
        }else {
            map.put("code",-1);
            map.put("message","查询失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> findGSpecificationsBysNorms(String sNorms) {
        Specifications g_specificationsBysNorms = specificationsMapper.findGSpecificationsBysNorms(sNorms);
        map = new HashMap<>();
        if (g_specificationsBysNorms!=null) {
            map.put("code",200);
            map.put("g_specifications",g_specificationsBysNorms);
        }else {
            map.put("code",-1);
            map.put("message","查询失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> updateGSpecificationsById(Specifications specifications) {
        Integer ret = specificationsMapper.updateGSpecificationsById(specifications);
        map = new HashMap<>();
        if (ret>0){
            delCache();
            redisUtil.del(redisUtil.keysLike("goodsAll"));
            map.put("code",200);
            map.put("message","更新成功");
        }else {
            map.put("code",-1);
            map.put("message","更新失败");
        }
        return map;
    }

    @Override
    public Map<String, Object> findSpecificationsBysNum(String sNum) {
        Specifications gSpecificationsSNum = specificationsMapper.findGSpecificationsSNum(sNum);
        Map<String,Object> map = new HashMap<>();
        if (gSpecificationsSNum!=null){
            map.put("code",200);
            map.put("msg","序号"+sNum+"已存在，请重新输入！");
        }else {
            map.put("code",-1);
            map.put("msg",sNum+"不存在");
        }
        return map;
    }

    @Override
    public Map<String, Object> findSpecificationsBysNorms(String sNorms) {
        Specifications specificationsBysNorms = specificationsMapper.findSpecificationsBysNorms(sNorms);
        Map<String,Object> map = new HashMap<>();
        if (specificationsBysNorms!=null){
            map.put("code",200);
            map.put("msg","型号"+sNorms+"已存在，请重新输入！");
        }else {
            map.put("code",-1);
            map.put("msg",sNorms+"不存在");
        }
        return map;
    }

    public void delCache(){
        redisUtil.del(redisUtil.keysLike("allGSpecifications"));
    }
}
